package com.spring.cootask.web.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.cootask.web.model.FeTask;
import com.spring.cootask.web.model.FeTaskUser;
import com.spring.cootask.web.model.FeTaskUserTable;
import com.spring.cootask.web.model.FeTaskUserTableByBase;
import com.spring.cootask.web.model.FeTaskUserWithBLOBs;
import com.spring.cootask.web.service.CooTaskService;
import com.spring.cootask.web.service.TaskTransService;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;

import org.codehaus.jackson.map.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by geZhangyuan on 2016/9/20.
 */
@Controller
@RequestMapping(value = "/tasktrans")
public class TaskTransController {
   
	@Resource
    private TaskTransService taskTransService;
	
	@Resource
    private CooTaskService cooTaskService;

	@RequestMapping(value = "/transInfo")
	public ModelAndView transIndex(String taskID,Model model,HttpServletRequest request){
		model.addAttribute("taskID", taskID);
		return new ModelAndView("main/fetask/task/transinfo");
	}
	
   // @OaUserPermission(value = "/tasktrans/dataIndex")
    @RequestMapping(value = "/dataIndex")
    public ModelAndView dataIndex(String taskUserID,Integer isPlan,Model model,HttpServletRequest request){
/*    	taskUserID="e0e698ff-a6be-4673-b4e1-33ed320bff61";
    	isPlan=0;*/
//    	taskUserID="6cf3c681-92a3-429d-b3df-4ec989e1c9f2";
    //	isPlan=1;
    	
		List<FeTaskUserTable> list = null;
		try{
			list = taskTransService.feTaskUserTable(taskUserID,"");
		}catch(Exception e){
			e.printStackTrace();
		}
    	String data=JSONObject.toJSONString(list);
    	model.addAttribute("data",data);
    	model.addAttribute("taskUserID",taskUserID);
    	model.addAttribute("isPlan",isPlan);
		FeTaskUserWithBLOBs feTaskUser=null;
		try{
			feTaskUser=taskTransService.feTaskUser(taskUserID);
		}catch(Exception e){
			e.printStackTrace();
		}
    	//用户自己的计划
		checkData(model,isPlan,feTaskUser);
    	model.addAttribute("isPlan", isPlan);
        return new ModelAndView("main/cootask/task/dataIndex");
    }

	// @OaUserPermission(value = "/tasktrans/dataIndex")
	@RequestMapping(value = "/dataIndexShow")
	public ModelAndView dataIndexShow(String taskUserID,Integer isPlan,Model model,HttpServletRequest request){
/*    	taskUserID="e0e698ff-a6be-4673-b4e1-33ed320bff61";
    	isPlan=0;*/
//    	taskUserID="6cf3c681-92a3-429d-b3df-4ec989e1c9f2";
		//	isPlan=1;
		List<FeTaskUserTable> list = null;
		try{
			list = taskTransService.feTaskUserTable(taskUserID,"");
		}catch(Exception e){
			e.printStackTrace();
		}
		String data=JSONObject.toJSONString(list);
		model.addAttribute("data",data);
		model.addAttribute("taskUserID",taskUserID);
		model.addAttribute("isPlan",isPlan);

		FeTaskUserWithBLOBs feTaskUser=null;
		try{
			feTaskUser=taskTransService.feTaskUser(taskUserID);
		}catch(Exception e){
			e.printStackTrace();
		}
		checkData(model,isPlan,feTaskUser);
		model.addAttribute("isPlan", isPlan);
		return new ModelAndView("main/cootask/task/dataIndex_show");
	}

	private void checkData(Model model,Integer isPlan,FeTaskUserWithBLOBs feTaskUser){
		//用户自己的计划
		if(isPlan==0){
			if(feTaskUser!=null){
				model.addAttribute("oldData",feTaskUser.getUserDataJson());
				model.addAttribute("planData",feTaskUser.getPlanDataJson());
			}else{
				model.addAttribute("oldData",null);
				model.addAttribute("planData",null);
			}
		}else{
			//计划
			if(feTaskUser!=null){
				model.addAttribute("oldData",feTaskUser.getPlanDataJson());
			}else{
				model.addAttribute("oldData",null);
			}
		}
	}
    
    @RequestMapping(value = "/feTaskUser", method = RequestMethod.POST)
    @ResponseBody
    public Object feTaskUser(@RequestBody FeTaskUserWithBLOBs feTaskUserWithBLOBs,HttpServletRequest request){
        BaseResult br=null;		
    	try{    		
			br=taskTransService.feTaskUser(feTaskUserWithBLOBs); 		
    	}catch(Exception e){
    		br=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
		return br;
    }
	/**
	 * 获取OA下级部门及人员接口
	 * @param callback
	 * @return
	 */
	@RequestMapping(value = "/easyTreeDeptUser")
	@ResponseBody
	public Object easyTreeDeptUser(String taskID,String dept_id,String callback) {
		List<Map<String, Object>> m = taskTransService.easyTreeDeptUser(taskID,dept_id);
		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}
	}

	
	@RequestMapping(value = "/feTaskUserTable",method = RequestMethod.GET)
	@ResponseBody
	public Object feTaskUserTable(String taskUserID) {		
		BaseResult rt = null;
    	try{
    		List<FeTaskUserTable> list = taskTransService.feTaskUserTable(taskUserID,"");
    		rt=new BaseResult("1","查询成功",list);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
	}	
/*	
	@RequestMapping(value = "/saveFeTaskUserTable",method = RequestMethod.POST)
	@ResponseBody
	public Object feTaskUserTable(@RequestBody List<FeTaskUserTable> list) {		
		BaseResult rt = null;
    	try{
    		
    		rt = taskTransService.feTaskUserTable(list);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
	}*/
	
	@RequestMapping(value = "/feTaskUserTableByBase",method = RequestMethod.POST)
	@ResponseBody
	public Object feTaskUserTableByBase(@RequestBody FeTaskUserTableByBase feTaskUserTableByBase) {		
		BaseResult rt = null;
    	try{   		
    		rt = taskTransService.feTaskUserTableByBase(feTaskUserTableByBase);
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
	}

	/**
	 * 查询任务树
	 * @param taskID
	 * @param page
	 * @param rows
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/transInfoList",method = RequestMethod.GET)
	@ResponseBody
	public Object transInfoList(String taskID,String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<FeTask> list = taskTransService.trans(taskID);
        PageInfo<FeTask> pageObj = new PageInfo<FeTask>(list);
        
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	@RequestMapping(value = "/transDetail",method = RequestMethod.GET)
	@ResponseBody
	public Object transDetail(String taskID,String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();
	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
    	List<FeTaskUserWithBLOBs> list = taskTransService.transDetail(taskID);
        PageInfo<FeTaskUserWithBLOBs> pageObj = new PageInfo<FeTaskUserWithBLOBs>(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	
	@RequestMapping(value = "/transDataDetail",method = RequestMethod.GET)
	@ResponseBody
	public Object transDataDetail(String taskUserID,String page,String rows,HttpServletRequest request){
	    HashMap<String,Object> data=new HashMap<String,Object>();
	    
    	List<FeTaskUserTable> metadata=taskTransService.feTaskUserTable(taskUserID,"");
	    
    	FeTaskUserWithBLOBs feTaskUser=taskTransService.feTaskUser(taskUserID);
    	
	    data.put("msg", "查询成功");
	    data.put("status", "1");
	    data.put("metadata", metadata);
	    data.put("data", feTaskUser.getUserDataJson());	    

	    return data;
	} 

    @RequestMapping(value = "/saveFormData")
    @ResponseBody
    public Object saveFormData(@RequestBody List<Map<String, Object>> jsonData, HttpServletRequest request) {
        BaseResult ret = null;
        try {
            ShopOaSessionUtil.OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
            ret = taskTransService.saveFormData(oaUser, jsonData);
        } catch (Exception e) {
            e.printStackTrace();
            ret.setStatus("0");
            ret.setMsg("系统异常");
        }
        return ret;
    }
    
    @RequestMapping(value = "/updateTaskUser")
    @ResponseBody
    public Object updateTaskUser(FeTaskUserWithBLOBs feTaskUser, HttpServletRequest request) {
        BaseResult ret = null;
        try {
            ret = taskTransService.updateTaskUser(feTaskUser);
        } catch (Exception e) {
            e.printStackTrace();
            ret.setStatus("0");
            ret.setMsg("系统异常");
        }
        return ret;
    }
   
    @RequestMapping(value = "/getBitMatrix", method = RequestMethod.GET)
    public void getBitMatrix(Integer isGrid,String taskID, HttpServletRequest request, HttpServletResponse response) throws IOException {
    	response.setContentType("image/gif");

    	String location=JedisCont.getProParm("ipad.user_url");
    	String type=(isGrid==1?"fullpagegrid":"fullpage");   	
    	String url =location+"/SperaOther/other/fetask/"+type+"?taskId="+taskID;
    	
    	BufferedImage bufferedImage=null;
        try {
        	bufferedImage =MyTools.createBitMatrix(url);
        	OutputStream stream = response.getOutputStream();
        	ImageIO.write(bufferedImage, "gif", stream);  
        	stream.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
